<template>
  <div class="login-container">
    <div class="login-background">
      <div class="particles">
        <div v-for="i in 30" :key="i" class="particle" :style="particleStyle(i)"></div>
      </div>
      <div class="tech-circle"></div>
      <div class="tech-line"></div>
    </div>

    <el-card class="login-card">
      <div class="logo">
        <div class="logo-icon">
          <div class="cube">
            <div class="front"></div>
            <div class="back"></div>
            <div class="left"></div>
            <div class="right"></div>
            <div class="top"></div>
            <div class="bottom"></div>
          </div>
          <div class="scan-line"></div>
        </div>
        <span class="title">仓库管理系统</span>
      </div>

      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              type="password"
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              @click="handleLogin"
              class="login-btn"
          >
            登 录
          </el-button>
        </el-form-item>
        <div class="footer">
          <el-button type="text" @click="goToRegister">没有账号？立即注册</el-button>
          <el-button type="text" @click="forgotPassword">忘记密码</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    particleStyle() {
      const size = Math.random() * 5 + 1;
      return {
        width: `${size}px`,
        height: `${size}px`,
        left: `${Math.random() * 100}%`,
        top: `${Math.random() * 100}%`,
        animationDelay: `${Math.random() * 5}s`
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          request.post('/user/login', this.loginForm)
              .then(response => {
                if (response.data.code === '200') {
                  localStorage.setItem('token', response.data.data.token)
                  localStorage.setItem('userInfo', JSON.stringify(response.data.data.userInfo))
                  //单独保存用户角色信息
                  localStorage.setItem('roleId', response.data.data.userInfo.roleId)
                  this.$message.success('登录成功')
                  this.$router.push('/home')
                } else if(response.data.code === '000') {
                  this.$message.error('用户名或密码错误')
                } else {
                  this.$message.error(response.data.msg || '登录失败')
                }
              })
              .catch(error => {
                console.error('登录失败:', error)
                this.$message.error('登录失败，请稍后重试')
              })
        }
      })
    },
    goToRegister() {
      this.$router.push('/register')
    },
    forgotPassword() {
      this.$router.push('/forgot-password')
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #141e30, #243b55);
  position: relative;
  overflow: hidden;
}

.login-background {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.particles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.particle {
  position: absolute;
  background-color: rgba(0, 210, 255, 0.5);
  border-radius: 50%;
  animation: float 15s infinite linear;
}

.tech-circle {
  position: absolute;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  border: 2px dashed rgba(0, 210, 255, 0.2);
  right: -100px;
  bottom: -100px;
  animation: rotate 30s linear infinite;
}

.tech-line {
  position: absolute;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(0, 210, 255, 0.3), transparent);
  top: 50%;
  box-shadow: 0 0 10px rgba(0, 210, 255, 0.3);
}

.login-card {
  width: 420px;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1;
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo-icon {
  position: relative;
  width: 60px;
  height: 60px;
  perspective: 1000px;
  margin-bottom: 15px;
}

.cube {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  animation: rotate 15s infinite linear;
}

.cube div {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #3a7bd5, #00d2ff);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 0 15px rgba(58, 123, 213, 0.7);
  opacity: 0.8;
}

.front { transform: translateZ(30px); }
.back { transform: translateZ(-30px); }
.left { transform: rotateY(-90deg) translateZ(30px); }
.right { transform: rotateY(90deg) translateZ(30px); }
.top { transform: rotateX(90deg) translateZ(30px); }
.bottom { transform: rotateX(-90deg) translateZ(30px); }

.scan-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.8), transparent);
  animation: scan 3s infinite linear;
  box-shadow: 0 0 15px #fff;
  z-index: 2;
}

.title {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 2px;
  color: #fff;
  text-shadow: 0 0 10px rgba(0, 210, 255, 0.7);
}

/deep/ .el-form-item__label {
  color: #fff !important;
  font-weight: 400;
}

/deep/ .el-input__inner {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.2) !important;
  color: #fff !important;
  height: 48px;
  border-radius: 6px;
}

/deep/ .el-input__inner::placeholder {
  color: rgba(255, 255, 255, 0.5) !important;
}

/deep/ .el-input__prefix {
  color: rgba(255, 255, 255, 0.7) !important;
}

.login-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(90deg, #3a7bd5, #00d2ff) !important;
  border: none !important;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 2px;
  margin-top: 10px;
  transition: all 0.3s;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(58, 123, 213, 0.4);
}

.footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

/deep/ .el-button--text {
  color: rgba(255, 255, 255, 0.7) !important;
}

/deep/ .el-button--text:hover {
  color: #00d2ff !important;
}

@keyframes rotate {
  0% { transform: rotateX(0) rotateY(0); }
  100% { transform: rotateX(360deg) rotateY(360deg); }
}

@keyframes scan {
  0% { top: -10px; opacity: 0; }
  10% { opacity: 1; }
  90% { opacity: 1; }
  100% { top: 100%; opacity: 0; }
}

@keyframes float {
  0% { transform: translateY(0) translateX(0); }
  50% { transform: translateY(-100px) translateX(20px); }
  100% { transform: translateY(0) translateX(0); }
}
</style>